home *** CD-ROM | disk | FTP | other *** search
/ Micromanía 92 / CDMM92_1.ISO / SOF 2 SDK / sof2sdk-101.msi / _92D6AC311BB48EBA344BBABC89DA6AB0 / _4B3EF0A7CFD543DEBF310C49488E6C7E < prev    next >
Encoding:
Text File  |  2002-06-17  |  40.7 KB  |  1,666 lines

  1. // Copyright (C) 2001-2002 Raven Software
  2. //
  3.  
  4. #include "g_local.h"
  5.  
  6.  
  7.  
  8. /*
  9. ===============================================================================
  10.  
  11. PUSHMOVE
  12.  
  13. ===============================================================================
  14. */
  15.  
  16. void MatchTeam( gentity_t *teamLeader, int moverState, int time );
  17.  
  18. typedef struct {
  19.     gentity_t    *ent;
  20.     vec3_t    origin;
  21.     vec3_t    angles;
  22.     float    deltayaw;
  23. } pushed_t;
  24. pushed_t    pushed[MAX_GENTITIES], *pushed_p;
  25.  
  26.  
  27. /*
  28. ============
  29. G_TestEntityPosition
  30.  
  31. ============
  32. */
  33. gentity_t    *G_TestEntityPosition( gentity_t *ent ) {
  34.     trace_t    tr;
  35.     int        mask;
  36.  
  37.     if ( ent->clipmask ) {
  38.         mask = ent->clipmask;
  39.     } else {
  40.         mask = MASK_SOLID;
  41.     }
  42.     if ( ent->client ) {
  43.         trap_Trace( &tr, ent->client->ps.origin, ent->r.mins, ent->r.maxs, ent->client->ps.origin, ent->s.number, mask );
  44.     } else {
  45.         trap_Trace( &tr, ent->s.pos.trBase, ent->r.mins, ent->r.maxs, ent->s.pos.trBase, ent->s.number, mask );
  46.     }
  47.     
  48.     if (tr.startsolid)
  49.         return &g_entities[ tr.entityNum ];
  50.         
  51.     return NULL;
  52. }
  53.  
  54. /*
  55. ================
  56. G_CreateRotationMatrix
  57. ================
  58. */
  59. void G_CreateRotationMatrix(vec3_t angles, vec3_t matrix[3]) {
  60.     AngleVectors(angles, matrix[0], matrix[1], matrix[2]);
  61.     VectorInverse(matrix[1]);
  62. }
  63.  
  64. /*
  65. ================
  66. G_TransposeMatrix
  67. ================
  68. */
  69. void G_TransposeMatrix(vec3_t matrix[3], vec3_t transpose[3]) {
  70.     int i, j;
  71.     for (i = 0; i < 3; i++) {
  72.         for (j = 0; j < 3; j++) {
  73.             transpose[i][j] = matrix[j][i];
  74.         }
  75.     }
  76. }
  77.  
  78. /*
  79. ================
  80. G_RotatePoint
  81. ================
  82. */
  83. void G_RotatePoint(vec3_t point, vec3_t matrix[3]) {
  84.     vec3_t tvec;
  85.  
  86.     VectorCopy(point, tvec);
  87.     point[0] = DotProduct(matrix[0], tvec);
  88.     point[1] = DotProduct(matrix[1], tvec);
  89.     point[2] = DotProduct(matrix[2], tvec);
  90. }
  91.  
  92. /*
  93. ==================
  94. G_TryPushingEntity
  95.  
  96. Returns qfalse if the move is blocked
  97. ==================
  98. */
  99. qboolean G_TryPushingEntity( gentity_t *check, gentity_t *pusher, vec3_t move, vec3_t amove ) 
  100. {
  101.     vec3_t        matrix[3], transpose[3];
  102.     vec3_t        org, org2, move2;
  103.     gentity_t    *block;
  104.  
  105.     // EF_MOVER_STOP will just stop when contacting another entity
  106.     // instead of pushing it, but entities can still ride on top of it
  107.     if ( ( pusher->s.eFlags & EF_MOVER_STOP ) && check->s.groundEntityNum != pusher->s.number ) 
  108.     {
  109.         return qfalse;
  110.     }
  111.  
  112.     // save off the old position
  113.     if (pushed_p > &pushed[MAX_GENTITIES]) 
  114.     {
  115.         Com_Error ( ERR_FATAL, "pushed_p > &pushed[MAX_GENTITIES]" );
  116.     }
  117.  
  118.     pushed_p->ent = check;
  119.     VectorCopy (check->s.pos.trBase, pushed_p->origin);
  120.     VectorCopy (check->s.apos.trBase, pushed_p->angles);
  121.     if ( check->client ) 
  122.     {
  123.         pushed_p->deltayaw = check->client->ps.delta_angles[YAW];
  124.         VectorCopy (check->client->ps.origin, pushed_p->origin);
  125.     }
  126.  
  127.     pushed_p++;
  128.  
  129.     // try moving the contacted entity 
  130.     // figure movement due to the pusher's amove
  131.     G_CreateRotationMatrix( amove, transpose );
  132.     G_TransposeMatrix( transpose, matrix );
  133.     if ( check->client ) 
  134.     {
  135.         VectorSubtract (check->client->ps.origin, pusher->r.currentOrigin, org);
  136.     }
  137.     else 
  138.     {
  139.         VectorSubtract (check->s.pos.trBase, pusher->r.currentOrigin, org);
  140.     }
  141.  
  142.     VectorCopy( org, org2 );
  143.     G_RotatePoint( org2, matrix );
  144.     VectorSubtract (org2, org, move2);
  145.     
  146.     // add movement
  147.     VectorAdd (check->s.pos.trBase, move, check->s.pos.trBase);
  148.     VectorAdd (check->s.pos.trBase, move2, check->s.pos.trBase);
  149.     if ( check->client ) 
  150.     {
  151.         VectorAdd (check->client->ps.origin, move, check->client->ps.origin);
  152.         VectorAdd (check->client->ps.origin, move2, check->client->ps.origin);
  153.         // make sure the client's view rotates when on a rotating mover
  154.         check->client->ps.delta_angles[YAW] += ANGLE2SHORT(amove[YAW]);
  155.     }
  156.  
  157.     // may have pushed them off an edge
  158.     if ( check->s.groundEntityNum != pusher->s.number ) 
  159.     {
  160.         check->s.groundEntityNum = -1;
  161.     }
  162.  
  163.     block = G_TestEntityPosition( check );
  164.     if (!block) 
  165.     {
  166.         // pushed ok
  167.         if ( check->client ) 
  168.         {
  169.             VectorCopy( check->client->ps.origin, check->r.currentOrigin );
  170.         } 
  171.         else 
  172.         {
  173.             VectorCopy( check->s.pos.trBase, check->r.currentOrigin );
  174.         }
  175.         
  176.         trap_LinkEntity (check);
  177.         return qtrue;
  178.     }
  179.  
  180.     // if it is ok to leave in the old position, do it
  181.     // this is only relevent for riding entities, not pushed
  182.     // Sliding trapdoors can cause this.
  183.     VectorCopy( (pushed_p-1)->origin, check->s.pos.trBase);
  184.     if ( check->client ) 
  185.     {
  186.         VectorCopy( (pushed_p-1)->origin, check->client->ps.origin);
  187.     }
  188.  
  189.     VectorCopy( (pushed_p-1)->angles, check->s.apos.trBase );
  190.     block = G_TestEntityPosition (check);
  191.     if ( !block ) 
  192.     {
  193.         check->s.groundEntityNum = -1;
  194.         pushed_p--;
  195.         return qtrue;
  196.     }
  197.  
  198.     // blocked
  199.     return qfalse;
  200. }
  201.  
  202.  
  203. void G_ExplodeMissile( gentity_t *ent );
  204.  
  205. /*
  206. ============
  207. G_MoverPush
  208.  
  209. Objects need to be moved back on a failed push,
  210. otherwise riders would continue to slide.
  211. If qfalse is returned, *obstacle will be the blocking entity
  212. ============
  213. */
  214. qboolean G_MoverPush( gentity_t *pusher, vec3_t move, vec3_t amove, gentity_t **obstacle ) {
  215.     int            i, e;
  216.     gentity_t    *check;
  217.     vec3_t        mins, maxs;
  218.     pushed_t    *p;
  219.     int            entityList[MAX_GENTITIES];
  220.     int            listedEntities;
  221.     vec3_t        totalMins, totalMaxs;
  222.  
  223.     *obstacle = NULL;
  224.  
  225.  
  226.     // mins/maxs are the bounds at the destination
  227.     // totalMins / totalMaxs are the bounds for the entire move
  228.     if ( pusher->r.currentAngles[0] || pusher->r.currentAngles[1] || pusher->r.currentAngles[2]
  229.         || amove[0] || amove[1] || amove[2] ) 
  230.     {
  231.         float        radius;
  232.  
  233.         radius = RadiusFromBounds( pusher->r.mins, pusher->r.maxs );
  234.         for ( i = 0 ; i < 3 ; i++ ) 
  235.         {
  236.             mins[i] = pusher->r.currentOrigin[i] + move[i] - radius;
  237.             maxs[i] = pusher->r.currentOrigin[i] + move[i] + radius;
  238.             totalMins[i] = mins[i] - move[i];
  239.             totalMaxs[i] = maxs[i] - move[i];
  240.         }
  241.  
  242.         // If only yaw then dontbother with the z axis
  243.         if ( !pusher->r.currentAngles[PITCH] && !pusher->r.currentAngles[ROLL] )
  244.         {
  245.             mins[2] = pusher->r.absmin[i] + move[2];
  246.             maxs[2] = pusher->r.absmax[i] + move[2];
  247.  
  248.             totalMins[2] = pusher->r.absmin[2];
  249.             totalMaxs[2] = pusher->r.absmax[2];
  250.  
  251.             if ( move[2] > 0 )
  252.             {
  253.                 totalMaxs[2] += move[i];
  254.             }
  255.             else
  256.             {
  257.                 totalMins[2] += move[i];
  258.             }
  259.         }
  260.     } 
  261.     else 
  262.     {
  263.         for (i=0 ; i<3 ; i++) {
  264.             mins[i] = pusher->r.absmin[i] + move[i];
  265.             maxs[i] = pusher->r.absmax[i] + move[i];
  266.         }
  267.  
  268.         VectorCopy( pusher->r.absmin, totalMins );
  269.         VectorCopy( pusher->r.absmax, totalMaxs );
  270.         for (i=0 ; i<3 ; i++) {
  271.             if ( move[i] > 0 ) {
  272.                 totalMaxs[i] += move[i];
  273.             } else {
  274.                 totalMins[i] += move[i];
  275.             }
  276.         }
  277.     }
  278.  
  279.     // unlink the pusher so we don't get it in the entityList
  280.     trap_UnlinkEntity( pusher );
  281.  
  282.     listedEntities = trap_EntitiesInBox( totalMins, totalMaxs, entityList, MAX_GENTITIES );
  283.  
  284.     // move the pusher to it's final position
  285.     VectorAdd( pusher->r.currentOrigin, move, pusher->r.currentOrigin );
  286.     VectorAdd( pusher->r.currentAngles, amove, pusher->r.currentAngles );
  287.     trap_LinkEntity( pusher );
  288.  
  289.     // see if any solid entities are inside the final position
  290.     for ( e = 0 ; e < listedEntities ; e++ ) {
  291.         check = &g_entities[ entityList[ e ] ];
  292.  
  293.         // Dont bother with corpses for now
  294.         if ( check->s.eType == ET_BODY )
  295.         {
  296.             continue;
  297.         }
  298.  
  299.         // only push items and players
  300.         if ( check->s.eType != ET_ITEM && check->s.eType != ET_PLAYER && !check->physicsObject ) {
  301.             continue;
  302.         }
  303.  
  304.         // if the entity is standing on the pusher, it will definitely be moved
  305.         if ( check->s.groundEntityNum != pusher->s.number ) {
  306.             // see if the ent needs to be tested
  307.             if ( check->r.absmin[0] >= maxs[0]
  308.             || check->r.absmin[1] >= maxs[1]
  309.             || check->r.absmin[2] >= maxs[2]
  310.             || check->r.absmax[0] <= mins[0]
  311.             || check->r.absmax[1] <= mins[1]
  312.             || check->r.absmax[2] <= mins[2] ) {
  313.                 continue;
  314.             }
  315.             // see if the ent's bbox is inside the pusher's final position
  316.             // this does allow a fast moving object to pass through a thin entity...
  317.             if (!G_TestEntityPosition (check)) {
  318.                 continue;
  319.             }
  320.         }
  321.  
  322.         // the entity needs to be pushed
  323.         if ( G_TryPushingEntity( check, pusher, move, amove ) ) 
  324.         {
  325.             continue;
  326.         }
  327.  
  328.         // If being blocked by an item just let it pass through it and get rid of the item
  329.         if ( check->s.eType == ET_ITEM && check->item->giType != IT_GAMETYPE )
  330.         {
  331.             G_FreeEntity ( check );            
  332.             continue;
  333.         }
  334.  
  335.  
  336.         // the move was blocked an entity
  337.  
  338.         // bobbing entities are instant-kill and never get blocked
  339.         if ( pusher->s.pos.trType == TR_SINE || pusher->s.apos.trType == TR_SINE ) 
  340.         {
  341.             G_Damage( check, pusher, pusher, NULL, NULL, 99999, 0, MOD_CRUSH, HL_NONE );
  342.             continue;
  343.         }
  344.  
  345.         
  346.         // save off the obstacle so we can call the block function (crush, etc)
  347.         *obstacle = check;
  348.  
  349.         // move back any entities we already moved
  350.         // go backwards, so if the same entity was pushed
  351.         // twice, it goes back to the original position
  352.         for ( p=pushed_p-1 ; p>=pushed ; p-- ) {
  353.             VectorCopy (p->origin, p->ent->s.pos.trBase);
  354.             VectorCopy (p->angles, p->ent->s.apos.trBase);
  355.             if ( p->ent->client ) {
  356.                 p->ent->client->ps.delta_angles[YAW] = p->deltayaw;
  357.                 VectorCopy (p->origin, p->ent->client->ps.origin);
  358.             }
  359.             trap_LinkEntity (p->ent);
  360.         }
  361.         return qfalse;
  362.     }
  363.  
  364.     return qtrue;
  365. }
  366.  
  367.  
  368. /*
  369. =================
  370. G_MoverTeam
  371. =================
  372. */
  373. void G_MoverTeam( gentity_t *ent ) {
  374.     vec3_t        move, amove;
  375.     gentity_t    *part, *obstacle;
  376.     vec3_t        origin, angles;
  377.  
  378.     obstacle = NULL;
  379.  
  380.     // make sure all team slaves can move before commiting
  381.     // any moves or calling any think functions
  382.     // if the move is blocked, all moved objects will be backed out
  383.     pushed_p = pushed;
  384.     for (part = ent ; part ; part=part->teamchain) {
  385.         // get current position
  386.         BG_EvaluateTrajectory( &part->s.pos, level.time, origin );
  387.         BG_EvaluateTrajectory( &part->s.apos, level.time, angles );
  388.         VectorSubtract( origin, part->r.currentOrigin, move );
  389.         VectorSubtract( angles, part->r.currentAngles, amove );
  390.         if ( !G_MoverPush( part, move, amove, &obstacle ) ) {
  391.             break;    // move was blocked
  392.         }
  393.     }
  394.  
  395.     if (part) {
  396.         // go back to the previous position
  397.         for ( part = ent ; part ; part = part->teamchain ) {
  398.             part->s.pos.trTime += level.time - level.previousTime;
  399.             part->s.apos.trTime += level.time - level.previousTime;
  400.             BG_EvaluateTrajectory( &part->s.pos, level.time, part->r.currentOrigin );
  401.             BG_EvaluateTrajectory( &part->s.apos, level.time, part->r.currentAngles );
  402.             trap_LinkEntity( part );
  403.         }
  404.  
  405.         // if the pusher has a "blocked" function, call it
  406.         if (ent->blocked) {
  407.             ent->blocked( ent, obstacle );
  408.         }
  409.         return;
  410.     }
  411.  
  412.     // the move succeeded
  413.     for ( part = ent ; part ; part = part->teamchain ) {
  414.         // call the reached function if time is at or past end point
  415.         if ( part->s.pos.trType == TR_LINEAR_STOP ) {
  416.             if ( level.time >= part->s.pos.trTime + part->s.pos.trDuration ) {
  417.                 if ( part->reached ) {
  418.                     part->reached( part );
  419.                 }
  420.             }
  421.         }
  422.     }
  423. }
  424.  
  425. /*
  426. ================
  427. G_RunMover
  428.  
  429. ================
  430. */
  431. void G_RunMover( gentity_t *ent ) {
  432.     // if not a team captain, don't do anything, because
  433.     // the captain will handle everything
  434.     if ( ent->flags & FL_TEAMSLAVE ) {
  435.         return;
  436.     }
  437.  
  438.     // if stationary at one of the positions, don't move anything
  439.     if ( ent->s.pos.trType != TR_STATIONARY || ent->s.apos.trType != TR_STATIONARY ) {
  440.         G_MoverTeam( ent );
  441.     }
  442.  
  443.     // check think function
  444.     G_RunThink( ent );
  445. }
  446.  
  447. /*
  448. ============================================================================
  449.  
  450. GENERAL MOVERS
  451.  
  452. Doors, plats, and buttons are all binary (two position) movers
  453. Pos1 is "at rest", pos2 is "activated"
  454. ============================================================================
  455. */
  456.  
  457. /*
  458. ===============
  459. SetMoverState
  460. ===============
  461. */
  462. void SetMoverState( gentity_t *ent, moverState_t moverState, int time ) {
  463.     vec3_t            delta;
  464.     float            f;
  465.  
  466.     ent->moverState = moverState;
  467.  
  468.     ent->s.pos.trTime = time;
  469.     switch( moverState ) {
  470.     case MOVER_POS1:
  471.         VectorCopy( ent->pos1, ent->s.pos.trBase );
  472.         ent->s.pos.trType = TR_STATIONARY;
  473.         break;
  474.     case MOVER_POS2:
  475.         VectorCopy( ent->pos2, ent->s.pos.trBase );
  476.         ent->s.pos.trType = TR_STATIONARY;
  477.         break;
  478.     case MOVER_1TO2:
  479.         VectorCopy( ent->pos1, ent->s.pos.trBase );
  480.         VectorSubtract( ent->pos2, ent->pos1, delta );
  481.         f = 1000.0 / ent->s.pos.trDuration;
  482.         VectorScale( delta, f, ent->s.pos.trDelta );
  483.         ent->s.pos.trType = TR_LINEAR_STOP;
  484.         break;
  485.     case MOVER_2TO1:
  486.         VectorCopy( ent->pos2, ent->s.pos.trBase );
  487.         VectorSubtract( ent->pos1, ent->pos2, delta );
  488.         f = 1000.0 / ent->s.pos.trDuration;
  489.         VectorScale( delta, f, ent->s.pos.trDelta );
  490.         ent->s.pos.trType = TR_LINEAR_STOP;
  491.         break;
  492.     }
  493.     BG_EvaluateTrajectory( &ent->s.pos, level.time, ent->r.currentOrigin );    
  494.     trap_LinkEntity( ent );
  495. }
  496.  
  497. /*
  498. ================
  499. MatchTeam
  500.  
  501. All entities in a mover team will move from pos1 to pos2
  502. in the same amount of time
  503. ================
  504. */
  505. void MatchTeam( gentity_t *teamLeader, int moverState, int time ) {
  506.     gentity_t        *slave;
  507.  
  508.     for ( slave = teamLeader ; slave ; slave = slave->teamchain ) {
  509.         SetMoverState( slave, moverState, time );
  510.     }
  511. }
  512.  
  513.  
  514.  
  515. /*
  516. ================
  517. ReturnToPos1
  518. ================
  519. */
  520. void ReturnToPos1( gentity_t *ent ) {
  521.     MatchTeam( ent, MOVER_2TO1, level.time );
  522.  
  523.     // looping sound
  524.     ent->s.loopSound = ent->soundLoop;
  525.  
  526.     // starting sound
  527.     if ( ent->sound2to1 ) {
  528.         G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to1 );
  529.     }
  530. }
  531.  
  532.  
  533. /*
  534. ================
  535. Reached_BinaryMover
  536. ================
  537. */
  538. void Reached_BinaryMover( gentity_t *ent ) 
  539. {
  540.     // stop the looping sound
  541.     ent->s.loopSound = ent->soundLoop;
  542.  
  543.     if ( ent->moverState == MOVER_1TO2 ) 
  544.     {
  545.         // reached pos2
  546.         SetMoverState( ent, MOVER_POS2, level.time );
  547.  
  548.         // play sound
  549.         if ( ent->soundPos2 ) 
  550.         {
  551.             G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos2 );
  552.         }
  553.  
  554.         // return to pos1 after a delay
  555.         ent->think = ReturnToPos1;
  556.         ent->nextthink = level.time + ent->wait;
  557.  
  558.         // fire targets
  559.         if ( !ent->activator ) 
  560.         {
  561.             ent->activator = ent;
  562.         }
  563.  
  564.         G_UseTargets( ent, ent->activator );
  565.     } 
  566.     else if ( ent->moverState == MOVER_2TO1 ) 
  567.     {
  568.         // reached pos1
  569.         SetMoverState( ent, MOVER_POS1, level.time );
  570.  
  571.         // play sound
  572.         if ( ent->soundPos1 ) 
  573.         {
  574.             G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos1 );
  575.         }
  576.  
  577.         // close areaportals
  578.         if ( ent->teammaster == ent || !ent->teammaster ) 
  579.         {
  580.             trap_AdjustAreaPortalState( ent, qfalse );
  581.         }
  582.     } 
  583.     else 
  584.     {
  585.         Com_Error( ERR_FATAL, "Reached_BinaryMover: bad moverState" );
  586.     }
  587. }
  588.  
  589. /*
  590. ================
  591. Use_BinaryMover
  592. ================
  593. */
  594. void Use_BinaryMover( gentity_t *ent, gentity_t *other, gentity_t *activator ) 
  595. {
  596.     int        total;
  597.     int        partial;
  598.  
  599.     // only the master should be used
  600.     if ( ent->flags & FL_TEAMSLAVE ) 
  601.     {
  602.         Use_BinaryMover( ent->teammaster, other, activator );
  603.         return;
  604.     }
  605.  
  606.     ent->activator = activator;
  607.  
  608.     if ( ent->moverState == MOVER_POS1 ) 
  609.     {
  610.         // start moving 50 msec later, becase if this was player
  611.         // triggered, level.time hasn't been advanced yet
  612.         MatchTeam( ent, MOVER_1TO2, level.time + 50 );
  613.  
  614.         // starting sound
  615.         if ( ent->sound1to2 ) 
  616.         {
  617.             G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );
  618.         }
  619.  
  620.         // looping sound
  621.         ent->s.loopSound = ent->soundLoop;
  622.  
  623.         // open areaportal
  624.         if ( ent->teammaster == ent || !ent->teammaster ) 
  625.         {
  626.             trap_AdjustAreaPortalState( ent, qtrue );
  627.         }
  628.         return;
  629.     }
  630.  
  631.     // if all the way up, just delay before coming down
  632.     if ( ent->moverState == MOVER_POS2 ) 
  633.     {
  634.         ent->nextthink = level.time + ent->wait;
  635.         return;
  636.     }
  637.  
  638.     // only partway down before reversing
  639.     if ( ent->moverState == MOVER_2TO1 ) 
  640.     {
  641.         total = ent->s.pos.trDuration;
  642.         partial = level.time - ent->s.pos.trTime;
  643.         if ( partial > total ) 
  644.         {
  645.             partial = total;
  646.         }
  647.  
  648.         MatchTeam( ent, MOVER_1TO2, level.time - ( total - partial ) );
  649.  
  650.         if ( ent->sound1to2 ) 
  651.         {
  652.             G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );
  653.         }
  654.         return;
  655.     }
  656.  
  657.     // only partway up before reversing
  658.     if ( ent->moverState == MOVER_1TO2 ) 
  659.     {
  660.         total = ent->s.pos.trDuration;
  661.         partial = level.time - ent->s.pos.trTime;
  662.         if ( partial > total ) 
  663.         {
  664.             partial = total;
  665.         }
  666.  
  667.         MatchTeam( ent, MOVER_2TO1, level.time - ( total - partial ) );
  668.  
  669.         if ( ent->sound2to1 ) 
  670.         {
  671.             G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to1 );
  672.         }
  673.         return;
  674.     }
  675. }
  676.  
  677. /*
  678. ================
  679. InitMover
  680.  
  681. "pos1", "pos2", and "speed" should be set before calling,
  682. so the movement delta can be calculated
  683. ================
  684. */
  685. void InitMover( gentity_t *ent ) {
  686.     vec3_t        move;
  687.     float        distance;
  688.     char        *sound;
  689.  
  690.     // if the "model2" key is set, use a seperate model
  691.     // for drawing, but clip against the brushes
  692.     if ( ent->model2 ) {
  693.         ent->s.modelindex2 = G_ModelIndex( ent->model2 );
  694.     }
  695.  
  696.     // if the "loopsound" key is set, use a constant looping sound when moving
  697.     if ( G_SpawnString( "noise", "100", &sound ) ) {
  698.         ent->s.loopSound = G_SoundIndex( sound );
  699.     }
  700.  
  701.     ent->use = Use_BinaryMover;
  702.     ent->reached = Reached_BinaryMover;
  703.  
  704.     ent->moverState = MOVER_POS1;
  705.     ent->r.svFlags = SVF_USE_CURRENT_ORIGIN;
  706.     ent->s.eType = ET_MOVER;
  707.     VectorCopy (ent->pos1, ent->r.currentOrigin);
  708.     trap_LinkEntity (ent);
  709.  
  710.     ent->s.pos.trType = TR_STATIONARY;
  711.     VectorCopy( ent->pos1, ent->s.pos.trBase );
  712.  
  713.     // calculate time to reach second position from speed
  714.     VectorSubtract( ent->pos2, ent->pos1, move );
  715.     distance = VectorLength( move );
  716.     if ( ! ent->speed ) {
  717.         ent->speed = 100;
  718.     }
  719.     VectorScale( move, ent->speed, ent->s.pos.trDelta );
  720.     ent->s.pos.trDuration = distance * 1000 / ent->speed;
  721.     if ( ent->s.pos.trDuration <= 0 ) {
  722.         ent->s.pos.trDuration = 1;
  723.     }
  724. }
  725.  
  726.  
  727. /*
  728. ===============================================================================
  729.  
  730. DOOR
  731.  
  732. A use can be triggered either by a touch function, by being shot, or by being
  733. targeted by another entity.
  734.  
  735. ===============================================================================
  736. */
  737.  
  738. /*
  739. ================
  740. Blocked_Door
  741. ================
  742. */
  743. void Blocked_Door( gentity_t *ent, gentity_t *other ) 
  744. {
  745.     // remove anything other than a client
  746.     if ( !other->client ) 
  747.     {
  748.         // except mission items
  749.         if( other->s.eType == ET_ITEM && other->item->giType == IT_GAMETYPE ) 
  750.         {
  751.             return;
  752.         }
  753.  
  754.         G_TempEntity( other->s.origin, EV_ITEM_POP );
  755.         G_FreeEntity( other );
  756.         return;
  757.     }
  758.  
  759.     if ( ent->damage ) 
  760.     {
  761.         G_Damage( other, ent, ent, NULL, NULL, ent->damage, 0, MOD_CRUSH, HL_NONE );
  762.     }
  763.  
  764.     if ( ent->spawnflags & 4 ) 
  765.     {
  766.         // crushers don't reverse
  767.         return;        
  768.     }
  769.  
  770.     // reverse direction
  771.     Use_BinaryMover( ent, ent, other );
  772. }
  773.  
  774. /*
  775. ================
  776. Touch_DoorTriggerSpectator
  777. ================
  778. */
  779. static void Touch_DoorTriggerSpectator( gentity_t *ent, gentity_t *other, trace_t *trace ) {
  780.     int i, axis;
  781.     vec3_t origin, dir, angles;
  782.  
  783.     axis = ent->count;
  784.     VectorClear(dir);
  785.     if (fabs(other->s.origin[axis] - ent->r.absmax[axis]) <
  786.         fabs(other->s.origin[axis] - ent->r.absmin[axis])) {
  787.         origin[axis] = ent->r.absmin[axis] - 10;
  788.         dir[axis] = -1;
  789.     }
  790.     else {
  791.         origin[axis] = ent->r.absmax[axis] + 10;
  792.         dir[axis] = 1;
  793.     }
  794.     for (i = 0; i < 3; i++) {
  795.         if (i == axis) continue;
  796.         origin[i] = (ent->r.absmin[i] + ent->r.absmax[i]) * 0.5;
  797.     }
  798.     vectoangles(dir, angles);
  799.     TeleportPlayer(other, origin, angles );
  800. }
  801.  
  802. /*
  803. ================
  804. Touch_DoorTrigger
  805. ================
  806. */
  807. void Touch_DoorTrigger( gentity_t *ent, gentity_t *other, trace_t *trace ) 
  808. {
  809.     if ( other->client && !G_IsClientSpectating ( other->client ) ) 
  810.     {
  811.         // if the door is not open and not opening
  812.         if ( ent->parent->moverState != MOVER_1TO2 &&
  813.              ent->parent->moverState != MOVER_POS2) 
  814.         {
  815.             Touch_DoorTriggerSpectator( ent, other, trace );
  816.         }
  817.     }
  818.     else if ( ent->parent->moverState != MOVER_1TO2 ) 
  819.     {
  820.         Use_BinaryMover( ent->parent, ent, other );
  821.     }
  822. }
  823.  
  824. /*
  825. ======================
  826. Think_SpawnNewDoorTrigger
  827.  
  828. All of the parts of a door have been spawned, so create
  829. a trigger that encloses all of them
  830. ======================
  831. */
  832. void Think_SpawnNewDoorTrigger( gentity_t *ent ) 
  833. {
  834.     gentity_t    *other;
  835.     vec3_t        mins, maxs;
  836.     int            i, best;
  837.  
  838.     // set all of the slaves as shootable
  839.     for ( other = ent ; other ; other = other->teamchain ) {
  840.         other->takedamage = qtrue;
  841.     }
  842.  
  843.     // find the bounds of everything on the team
  844.     VectorCopy (ent->r.absmin, mins);
  845.     VectorCopy (ent->r.absmax, maxs);
  846.  
  847.     for (other = ent->teamchain ; other ; other=other->teamchain) {
  848.         AddPointToBounds (other->r.absmin, mins, maxs);
  849.         AddPointToBounds (other->r.absmax, mins, maxs);
  850.     }
  851.  
  852.     // find the thinnest axis, which will be the one we expand
  853.     best = 0;
  854.     for ( i = 1 ; i < 3 ; i++ ) {
  855.         if ( maxs[i] - mins[i] < maxs[best] - mins[best] ) {
  856.             best = i;
  857.         }
  858.     }
  859.     maxs[best] += 120;
  860.     mins[best] -= 120;
  861.  
  862.     // create a trigger with this size
  863.     other = G_Spawn ();
  864.     other->classname = "door_trigger";
  865.     VectorCopy (mins, other->r.mins);
  866.     VectorCopy (maxs, other->r.maxs);
  867.     other->parent = ent;
  868.     other->r.contents = CONTENTS_TRIGGER;
  869.     other->touch = Touch_DoorTrigger;
  870.     // remember the thinnest axis
  871.     other->count = best;
  872.     trap_LinkEntity (other);
  873.  
  874.     MatchTeam( ent, ent->moverState, level.time );
  875. }
  876.  
  877. void Think_MatchTeam( gentity_t *ent ) {
  878.     MatchTeam( ent, ent->moverState, level.time );
  879. }
  880.  
  881.  
  882. /*QUAKED func_door (0 .5 .8) ? START_OPEN x CRUSHER
  883. TOGGLE        wait in both the start and end states for a trigger event.
  884. START_OPEN    the door to moves to its destination when spawned, and operate in reverse.  It is used to temporarily or permanently close off an area when triggered (not useful for touch or takedamage doors).
  885. NOMONSTER    monsters will not trigger this door
  886.  
  887. "model2"    .md3 model to also draw
  888. "angle"        determines the opening direction
  889. "targetname" if set, no touch field will be spawned and a remote button or trigger field activates the door.
  890. "speed"        movement speed (100 default)
  891. "wait"        wait before returning (3 default, -1 = never return)
  892. "lip"        lip remaining at end of move (8 default)
  893. "dmg"        damage to inflict when blocked (2 default)
  894. "health"    if set, the door must be shot open
  895. */
  896. void SP_func_door (gentity_t *ent) {
  897.     vec3_t    abs_movedir;
  898.     float    distance;
  899.     vec3_t    size;
  900.     float    lip;
  901.  
  902.     ent->sound1to2 = ent->sound2to1 = G_SoundIndex("sound/movers/doors/dr1_strt.wav");
  903.     ent->soundPos1 = ent->soundPos2 = G_SoundIndex("sound/movers/doors/dr1_end.wav");
  904.  
  905.     ent->blocked = Blocked_Door;
  906.  
  907.     // default speed of 400
  908.     if (!ent->speed)
  909.         ent->speed = 400;
  910.  
  911.     // default wait of 2 seconds
  912.     if (!ent->wait)
  913.         ent->wait = 2;
  914.     ent->wait *= 1000;
  915.  
  916.     // default lip of 8 units
  917.     G_SpawnFloat( "lip", "8", &lip );
  918.  
  919.     // default damage of 2 points
  920.     G_SpawnInt( "dmg", "2", &ent->damage );
  921.  
  922.     // first position at start
  923.     VectorCopy( ent->s.origin, ent->pos1 );
  924.  
  925.     // calculate second position
  926.     trap_SetBrushModel( ent, ent->model );
  927.     G_SetMovedir (ent->s.angles, ent->movedir);
  928.     abs_movedir[0] = fabs(ent->movedir[0]);
  929.     abs_movedir[1] = fabs(ent->movedir[1]);
  930.     abs_movedir[2] = fabs(ent->movedir[2]);
  931.     VectorSubtract( ent->r.maxs, ent->r.mins, size );
  932.     distance = DotProduct( abs_movedir, size ) - lip;
  933.     VectorMA( ent->pos1, distance, ent->movedir, ent->pos2 );
  934.  
  935.     // if "start_open", reverse position 1 and 2
  936.     if ( ent->spawnflags & 1 ) {
  937.         vec3_t    temp;
  938.  
  939.         VectorCopy( ent->pos2, temp );
  940.         VectorCopy( ent->s.origin, ent->pos2 );
  941.         VectorCopy( temp, ent->pos1 );
  942.     }
  943.  
  944.     InitMover( ent );
  945.  
  946.     ent->nextthink = level.time + FRAMETIME;
  947.  
  948.     if ( ! (ent->flags & FL_TEAMSLAVE ) ) {
  949.         int health;
  950.  
  951.         G_SpawnInt( "health", "0", &health );
  952.         if ( health ) {
  953.             ent->takedamage = qtrue;
  954.         }
  955.         if ( ent->targetname || health ) {
  956.             // non touch/shoot doors
  957.             ent->think = Think_MatchTeam;
  958.         } else {
  959.             ent->think = Think_SpawnNewDoorTrigger;
  960.         }
  961.     }
  962.  
  963.  
  964. }
  965.  
  966. /*
  967. ===============================================================================
  968.  
  969. PLAT
  970.  
  971. ===============================================================================
  972. */
  973.  
  974. /*
  975. ==============
  976. Touch_Plat
  977.  
  978. Don't allow decent if a living player is on it
  979. ===============
  980. */
  981. void Touch_Plat( gentity_t *ent, gentity_t *other, trace_t *trace ) {
  982.     if ( !other->client || other->client->ps.stats[STAT_HEALTH] <= 0 ) {
  983.         return;
  984.     }
  985.  
  986.     // delay return-to-pos1 by one second
  987.     if ( ent->moverState == MOVER_POS2 ) {
  988.         ent->nextthink = level.time + 1000;
  989.     }
  990. }
  991.  
  992. /*
  993. ==============
  994. Touch_PlatCenterTrigger
  995.  
  996. If the plat is at the bottom position, start it going up
  997. ===============
  998. */
  999. void Touch_PlatCenterTrigger(gentity_t *ent, gentity_t *other, trace_t *trace ) {
  1000.     if ( !other->client ) {
  1001.         return;
  1002.     }
  1003.  
  1004.     if ( ent->parent->moverState == MOVER_POS1 ) {
  1005.         Use_BinaryMover( ent->parent, ent, other );
  1006.     }
  1007. }
  1008.  
  1009.  
  1010. /*
  1011. ================
  1012. SpawnPlatTrigger
  1013.  
  1014. Spawn a trigger in the middle of the plat's low position
  1015. Elevator cars require that the trigger extend through the entire low position,
  1016. not just sit on top of it.
  1017. ================
  1018. */
  1019. void SpawnPlatTrigger( gentity_t *ent ) {
  1020.     gentity_t    *trigger;
  1021.     vec3_t    tmin, tmax;
  1022.  
  1023.     // the middle trigger will be a thin trigger just
  1024.     // above the starting position
  1025.     trigger = G_Spawn();
  1026.     trigger->classname = "plat_trigger";
  1027.     trigger->touch = Touch_PlatCenterTrigger;
  1028.     trigger->r.contents = CONTENTS_TRIGGER;
  1029.     trigger->parent = ent;
  1030.     
  1031.     tmin[0] = ent->pos1[0] + ent->r.mins[0] + 33;
  1032.     tmin[1] = ent->pos1[1] + ent->r.mins[1] + 33;
  1033.     tmin[2] = ent->pos1[2] + ent->r.mins[2];
  1034.  
  1035.     tmax[0] = ent->pos1[0] + ent->r.maxs[0] - 33;
  1036.     tmax[1] = ent->pos1[1] + ent->r.maxs[1] - 33;
  1037.     tmax[2] = ent->pos1[2] + ent->r.maxs[2] + 8;
  1038.  
  1039.     if ( tmax[0] <= tmin[0] ) {
  1040.         tmin[0] = ent->pos1[0] + (ent->r.mins[0] + ent->r.maxs[0]) *0.5;
  1041.         tmax[0] = tmin[0] + 1;
  1042.     }
  1043.     if ( tmax[1] <= tmin[1] ) {
  1044.         tmin[1] = ent->pos1[1] + (ent->r.mins[1] + ent->r.maxs[1]) *0.5;
  1045.         tmax[1] = tmin[1] + 1;
  1046.     }
  1047.     
  1048.     VectorCopy (tmin, trigger->r.mins);
  1049.     VectorCopy (tmax, trigger->r.maxs);
  1050.  
  1051.     trap_LinkEntity (trigger);
  1052. }
  1053.  
  1054.  
  1055. /*QUAKED func_plat (0 .5 .8) ?
  1056. Plats are always drawn in the extended position so they will light correctly.
  1057.  
  1058. "lip"        default 8, protrusion above rest position
  1059. "height"    total height of movement, defaults to model height
  1060. "speed"        overrides default 200.
  1061. "dmg"        overrides default 2
  1062. "model2"    .md3 model to also draw
  1063. */
  1064. void SP_func_plat (gentity_t *ent) {
  1065.     float        lip, height;
  1066.  
  1067.     ent->sound1to2 = ent->sound2to1 = G_SoundIndex("sound/movers/plats/pt1_strt.wav");
  1068.     ent->soundPos1 = ent->soundPos2 = G_SoundIndex("sound/movers/plats/pt1_end.wav");
  1069.  
  1070.     VectorClear (ent->s.angles);
  1071.  
  1072.     G_SpawnFloat( "speed", "200", &ent->speed );
  1073.     G_SpawnInt( "dmg", "2", &ent->damage );
  1074.     G_SpawnFloat( "wait", "1", &ent->wait );
  1075.     G_SpawnFloat( "lip", "8", &lip );
  1076.  
  1077.     ent->wait = 1000;
  1078.  
  1079.     // create second position
  1080.     trap_SetBrushModel( ent, ent->model );
  1081.  
  1082.     if ( !G_SpawnFloat( "height", "0", &height ) ) {
  1083.         height = (ent->r.maxs[2] - ent->r.mins[2]) - lip;
  1084.     }
  1085.  
  1086.     // pos1 is the rest (bottom) position, pos2 is the top
  1087.     VectorCopy( ent->s.origin, ent->pos2 );
  1088.     VectorCopy( ent->pos2, ent->pos1 );
  1089.     ent->pos1[2] -= height;
  1090.  
  1091.     InitMover( ent );
  1092.  
  1093.     // touch function keeps the plat from returning while
  1094.     // a live player is standing on it
  1095.     ent->touch = Touch_Plat;
  1096.  
  1097.     ent->blocked = Blocked_Door;
  1098.  
  1099.     ent->parent = ent;    // so it can be treated as a door
  1100.  
  1101.     // spawn the trigger if one hasn't been custom made
  1102.     if ( !ent->targetname ) {
  1103.         SpawnPlatTrigger(ent);
  1104.     }
  1105. }
  1106.  
  1107.  
  1108. /*
  1109. ===============================================================================
  1110.  
  1111. BUTTON
  1112.  
  1113. ===============================================================================
  1114. */
  1115.  
  1116. /*
  1117. ==============
  1118. Touch_Button
  1119.  
  1120. ===============
  1121. */
  1122. void Touch_Button(gentity_t *ent, gentity_t *other, trace_t *trace ) {
  1123.     if ( !other->client ) {
  1124.         return;
  1125.     }
  1126.  
  1127.     if ( ent->moverState == MOVER_POS1 ) {
  1128.         Use_BinaryMover( ent, other, other );
  1129.     }
  1130. }
  1131.  
  1132.  
  1133. /*QUAKED func_button (0 .5 .8) ?
  1134. When a button is touched, it moves some distance in the direction of it's angle, triggers all of it's targets, waits some time, then returns to it's original position where it can be triggered again.
  1135.  
  1136. "model2"    .md3 model to also draw
  1137. "angle"        determines the opening direction
  1138. "target"    all entities with a matching targetname will be used
  1139. "speed"        override the default 40 speed
  1140. "wait"        override the default 1 second wait (-1 = never return)
  1141. "lip"        override the default 4 pixel lip remaining at end of move
  1142. "health"    if set, the button must be killed instead of touched
  1143. */
  1144. void SP_func_button( gentity_t *ent ) {
  1145.     vec3_t        abs_movedir;
  1146.     float        distance;
  1147.     vec3_t        size;
  1148.     float        lip;
  1149.  
  1150.     ent->sound1to2 = G_SoundIndex("sound/movers/switches/butn2.wav");
  1151.     
  1152.     if ( !ent->speed ) {
  1153.         ent->speed = 40;
  1154.     }
  1155.  
  1156.     if ( !ent->wait ) {
  1157.         ent->wait = 1;
  1158.     }
  1159.     ent->wait *= 1000;
  1160.  
  1161.     // first position
  1162.     VectorCopy( ent->s.origin, ent->pos1 );
  1163.  
  1164.     // calculate second position
  1165.     trap_SetBrushModel( ent, ent->model );
  1166.  
  1167.     G_SpawnFloat( "lip", "4", &lip );
  1168.  
  1169.     G_SetMovedir( ent->s.angles, ent->movedir );
  1170.     abs_movedir[0] = fabs(ent->movedir[0]);
  1171.     abs_movedir[1] = fabs(ent->movedir[1]);
  1172.     abs_movedir[2] = fabs(ent->movedir[2]);
  1173.     VectorSubtract( ent->r.maxs, ent->r.mins, size );
  1174.     distance = abs_movedir[0] * size[0] + abs_movedir[1] * size[1] + abs_movedir[2] * size[2] - lip;
  1175.     VectorMA (ent->pos1, distance, ent->movedir, ent->pos2);
  1176.  
  1177.     if (ent->health) {
  1178.         // shootable button
  1179.         ent->takedamage = qtrue;
  1180.     } else {
  1181.         // touchable button
  1182.         ent->touch = Touch_Button;
  1183.     }
  1184.  
  1185.     InitMover( ent );
  1186. }
  1187.  
  1188.  
  1189.  
  1190. /*
  1191. ===============================================================================
  1192.  
  1193. TRAIN
  1194.  
  1195. ===============================================================================
  1196. */
  1197.  
  1198.  
  1199. #define TRAIN_START_ON        1
  1200. #define TRAIN_TOGGLE        2
  1201. #define TRAIN_BLOCK_STOPS    4
  1202.  
  1203. /*
  1204. ===============
  1205. Think_BeginMoving
  1206.  
  1207. The wait time at a corner has completed, so start moving again
  1208. ===============
  1209. */
  1210. void Think_BeginMoving( gentity_t *ent ) {
  1211.     ent->s.pos.trTime = level.time;
  1212.     ent->s.pos.trType = TR_LINEAR_STOP;
  1213. }
  1214.  
  1215. /*
  1216. ===============
  1217. Reached_Train
  1218. ===============
  1219. */
  1220. void Reached_Train( gentity_t *ent ) {
  1221.     gentity_t        *next;
  1222.     float            speed;
  1223.     vec3_t            move;
  1224.     float            length;
  1225.  
  1226.     // copy the apropriate values
  1227.     next = ent->nextTrain;
  1228.     if ( !next || !next->nextTrain ) {
  1229.         return;        // just stop
  1230.     }
  1231.  
  1232.     // fire all other targets
  1233.     G_UseTargets( next, NULL );
  1234.  
  1235.     // set the new trajectory
  1236.     ent->nextTrain = next->nextTrain;
  1237.     VectorCopy( next->s.origin, ent->pos1 );
  1238.     VectorCopy( next->nextTrain->s.origin, ent->pos2 );
  1239.  
  1240.     // if the path_corner has a speed, use that
  1241.     if ( next->speed ) {
  1242.         speed = next->speed;
  1243.     } else {
  1244.         // otherwise use the train's speed
  1245.         speed = ent->speed;
  1246.     }
  1247.     if ( speed < 1 ) {
  1248.         speed = 1;
  1249.     }
  1250.  
  1251.     // calculate duration
  1252.     VectorSubtract( ent->pos2, ent->pos1, move );
  1253.     length = VectorLength( move );
  1254.  
  1255.     ent->s.pos.trDuration = length * 1000 / speed;
  1256.  
  1257.     // looping sound
  1258.     ent->s.loopSound = next->soundLoop;
  1259.  
  1260.     // start it going
  1261.     SetMoverState( ent, MOVER_1TO2, level.time );
  1262.  
  1263.     // if there is a "wait" value on the target, don't start moving yet
  1264.     if ( next->wait ) {
  1265.         ent->nextthink = level.time + next->wait * 1000;
  1266.         ent->think = Think_BeginMoving;
  1267.         ent->s.pos.trType = TR_STATIONARY;
  1268.     }
  1269. }
  1270.  
  1271.  
  1272. /*
  1273. ===============
  1274. Think_SetupTrainTargets
  1275.  
  1276. Link all the corners together
  1277. ===============
  1278. */
  1279. void Think_SetupTrainTargets( gentity_t *ent ) {
  1280.     gentity_t        *path, *next, *start;
  1281.  
  1282.     ent->nextTrain = G_Find( NULL, FOFS(targetname), ent->target );
  1283.     if ( !ent->nextTrain ) 
  1284.     {
  1285.         Com_Printf( "func_train at %s with an unfound target\n",
  1286.             vtos(ent->r.absmin) );
  1287.         return;
  1288.     }
  1289.  
  1290.     start = NULL;
  1291.     for ( path = ent->nextTrain ; path != start ; path = next ) {
  1292.         if ( !start ) {
  1293.             start = path;
  1294.         }
  1295.  
  1296.         if ( !path->target ) {
  1297.             Com_Printf( "Train corner at %s without a target\n",
  1298.                 vtos(path->s.origin) );
  1299.             return;
  1300.         }
  1301.  
  1302.         // find a path_corner among the targets
  1303.         // there may also be other targets that get fired when the corner
  1304.         // is reached
  1305.         next = NULL;
  1306.         do {
  1307.             next = G_Find( next, FOFS(targetname), path->target );
  1308.             if ( !next ) {
  1309.                 Com_Printf( "Train corner at %s without a target path_corner\n",
  1310.                     vtos(path->s.origin) );
  1311.                 return;
  1312.             }
  1313.         } while ( strcmp( next->classname, "path_corner" ) );
  1314.  
  1315.         path->nextTrain = next;
  1316.     }
  1317.  
  1318.     // start the train moving from the first corner
  1319.     Reached_Train( ent );
  1320. }
  1321.  
  1322.  
  1323.  
  1324. /*QUAKED path_corner (.5 .3 0) (-8 -8 -8) (8 8 8)
  1325. Train path corners.
  1326. Target: next path corner and other targets to fire
  1327. "speed" speed to move to the next corner
  1328. "wait" seconds to wait before behining move to next corner
  1329. */
  1330. void SP_path_corner( gentity_t *self ) {
  1331.     if ( !self->targetname ) {
  1332.         Com_Printf ("path_corner with no targetname at %s\n", vtos(self->s.origin));
  1333.         G_FreeEntity( self );
  1334.         return;
  1335.     }
  1336.     // path corners don't need to be linked in
  1337. }
  1338.  
  1339.  
  1340.  
  1341. /*QUAKED func_train (0 .5 .8) ? START_ON TOGGLE BLOCK_STOPS
  1342. A train is a mover that moves between path_corner target points.
  1343. Trains MUST HAVE AN ORIGIN BRUSH.
  1344. The train spawns at the first target it is pointing at.
  1345. "model2"    .md3 model to also draw
  1346. "speed"        default 100
  1347. "dmg"        default    2
  1348. "noise"        looping sound to play when the train is in motion
  1349. "target"    next path corner
  1350. */
  1351. void SP_func_train (gentity_t *self) {
  1352.     VectorClear (self->s.angles);
  1353.  
  1354.     if (self->spawnflags & TRAIN_BLOCK_STOPS) {
  1355.         self->damage = 0;
  1356.     } else {
  1357.         if (!self->damage) {
  1358.             self->damage = 2;
  1359.         }
  1360.     }
  1361.  
  1362.     if ( !self->speed ) {
  1363.         self->speed = 100;
  1364.     }
  1365.  
  1366.     if ( !self->target ) {
  1367.         Com_Printf ("func_train without a target at %s\n", vtos(self->r.absmin));
  1368.         G_FreeEntity( self );
  1369.         return;
  1370.     }
  1371.  
  1372.     trap_SetBrushModel( self, self->model );
  1373.     InitMover( self );
  1374.  
  1375.     self->reached = Reached_Train;
  1376.  
  1377.     // start trains on the second frame, to make sure their targets have had
  1378.     // a chance to spawn
  1379.     self->nextthink = level.time + FRAMETIME;
  1380.     self->think = Think_SetupTrainTargets;
  1381. }
  1382.  
  1383. /*
  1384. ===============================================================================
  1385.  
  1386. STATIC
  1387.  
  1388. ===============================================================================
  1389. */
  1390.  
  1391.  
  1392. /*QUAKED func_static (0 .5 .8) ?
  1393. A bmodel that just sits there, doing nothing.  Can be used for conditional walls and models.
  1394. "model2"    .md3 model to also draw
  1395. */
  1396. void SP_func_static( gentity_t *ent ) {
  1397.     trap_SetBrushModel( ent, ent->model );
  1398.     VectorCopy(ent->s.origin, ent->pos1);
  1399.     VectorCopy(ent->s.angles, ent->r.currentAngles);
  1400.     VectorCopy(ent->s.angles, ent->s.apos.trBase );
  1401.     InitMover( ent );
  1402.     VectorCopy( ent->s.origin, ent->s.pos.trBase );
  1403.     VectorCopy( ent->s.origin, ent->r.currentOrigin );
  1404.  
  1405.     if (level.mBSPInstanceDepth)
  1406.     {    // this means that this guy will never be updated, moved, changed, etc.
  1407.         ent->s.eFlags = EF_PERMANENT;
  1408.     }
  1409. }
  1410.  
  1411. void G_WallUse(gentity_t *self, gentity_t *other, gentity_t *activator)
  1412. {
  1413.     if ( self->r.linked )
  1414.     {
  1415.         trap_UnlinkEntity ( self );
  1416.     }
  1417.     else
  1418.     {
  1419.         trap_LinkEntity ( self );
  1420.     }
  1421. }
  1422.  
  1423. /*QUAKED func_wall (0 .5 .8) ? START_OFF
  1424. A func wall can be turned off and on by targetting it.
  1425. */
  1426. void SP_func_wall ( gentity_t* ent )
  1427. {
  1428.     // Right now func walls are not supported in multiplayer
  1429.     if ( RMG.integer )
  1430.     {
  1431.         trap_UnlinkEntity ( ent );
  1432.         return;
  1433.     }
  1434.  
  1435.     trap_SetBrushModel( ent, ent->model );
  1436.  
  1437.     InitMover( ent );
  1438.  
  1439.     VectorCopy( ent->s.origin, ent->s.pos.trBase );
  1440.     VectorCopy( ent->s.pos.trBase, ent->r.currentOrigin );
  1441.     VectorCopy( ent->s.apos.trBase, ent->r.currentAngles );
  1442.  
  1443.     ent->s.eType = ET_WALL;
  1444.  
  1445.     ent->use = G_WallUse;
  1446.  
  1447.     if ( !(ent->spawnflags & 1 ) )
  1448.     {
  1449.         trap_LinkEntity( ent );    
  1450.     }
  1451. }
  1452.  
  1453. /*
  1454. ===============================================================================
  1455.  
  1456. ROTATING
  1457.  
  1458. ===============================================================================
  1459. */
  1460.  
  1461.  
  1462. /*QUAKED func_rotating (0 .5 .8) ? START_ON - X_AXIS Y_AXIS
  1463. You need to have an origin brush as part of this entity.  The center of that brush will be
  1464. the point around which it is rotated. It will rotate around the Z axis by default.  You can
  1465. check either the X_AXIS or Y_AXIS box to change that.
  1466.  
  1467. "model2"    .md3 model to also draw
  1468. "speed"        determines how fast it moves; default value is 100.
  1469. "dmg"        damage to inflict when blocked (2 default)
  1470. */
  1471. void SP_func_rotating (gentity_t *ent) {
  1472.     if ( !ent->speed ) {
  1473.         ent->speed = 100;
  1474.     }
  1475.  
  1476.     // set the axis of rotation
  1477.     ent->s.apos.trType = TR_LINEAR;
  1478.     if ( ent->spawnflags & 4 ) {
  1479.         ent->s.apos.trDelta[2] = ent->speed;
  1480.     } else if ( ent->spawnflags & 8 ) {
  1481.         ent->s.apos.trDelta[0] = ent->speed;
  1482.     } else {
  1483.         ent->s.apos.trDelta[1] = ent->speed;
  1484.     }
  1485.  
  1486.     if (!ent->damage) {
  1487.         ent->damage = 2;
  1488.     }
  1489.  
  1490.     trap_SetBrushModel( ent, ent->model );
  1491.     InitMover( ent );
  1492.  
  1493.     VectorCopy( ent->s.origin, ent->s.pos.trBase );
  1494.     VectorCopy( ent->s.pos.trBase, ent->r.currentOrigin );
  1495.     VectorCopy( ent->s.apos.trBase, ent->r.currentAngles );
  1496.  
  1497.     trap_LinkEntity( ent );
  1498. }
  1499.  
  1500.  
  1501. /*
  1502. ===============================================================================
  1503.  
  1504. BOBBING
  1505.  
  1506. ===============================================================================
  1507. */
  1508.  
  1509.  
  1510. /*QUAKED func_bobbing (0 .5 .8) ? X_AXIS Y_AXIS
  1511. Normally bobs on the Z axis
  1512. "model2"    .md3 model to also draw
  1513. "height"    amplitude of bob (32 default)
  1514. "speed"        seconds to complete a bob cycle (4 default)
  1515. "phase"        the 0.0 to 1.0 offset in the cycle to start at
  1516. "dmg"        damage to inflict when blocked (2 default)
  1517. */
  1518. void SP_func_bobbing (gentity_t *ent) {
  1519.     float        height;
  1520.     float        phase;
  1521.  
  1522.     G_SpawnFloat( "speed", "4", &ent->speed );
  1523.     G_SpawnFloat( "height", "32", &height );
  1524.     G_SpawnInt( "dmg", "2", &ent->damage );
  1525.     G_SpawnFloat( "phase", "0", &phase );
  1526.  
  1527.     trap_SetBrushModel( ent, ent->model );
  1528.     InitMover( ent );
  1529.  
  1530.     VectorCopy( ent->s.origin, ent->s.pos.trBase );
  1531.     VectorCopy( ent->s.origin, ent->r.currentOrigin );
  1532.  
  1533.     ent->s.pos.trDuration = ent->speed * 1000;
  1534.     ent->s.pos.trTime = ent->s.pos.trDuration * phase;
  1535.     ent->s.pos.trType = TR_SINE;
  1536.  
  1537.     // set the axis of bobbing
  1538.     if ( ent->spawnflags & 1 ) {
  1539.         ent->s.pos.trDelta[0] = height;
  1540.     } else if ( ent->spawnflags & 2 ) {
  1541.         ent->s.pos.trDelta[1] = height;
  1542.     } else {
  1543.         ent->s.pos.trDelta[2] = height;
  1544.     }
  1545. }
  1546.  
  1547. /*
  1548. ===============================================================================
  1549.  
  1550. PENDULUM
  1551.  
  1552. ===============================================================================
  1553. */
  1554.  
  1555.  
  1556. /*QUAKED func_pendulum (0 .5 .8) ?
  1557. You need to have an origin brush as part of this entity.
  1558. Pendulums always swing north / south on unrotated models.  Add an angles field to the model to allow rotation in other directions.
  1559. Pendulum frequency is a physical constant based on the length of the beam and gravity.
  1560. "model2"    .md3 model to also draw
  1561. "speed"        the number of degrees each way the pendulum swings, (30 default)
  1562. "phase"        the 0.0 to 1.0 offset in the cycle to start at
  1563. "dmg"        damage to inflict when blocked (2 default)
  1564. */
  1565. void SP_func_pendulum(gentity_t *ent) {
  1566.     float        freq;
  1567.     float        length;
  1568.     float        phase;
  1569.     float        speed;
  1570.  
  1571.     G_SpawnFloat( "speed", "30", &speed );
  1572.     G_SpawnInt( "dmg", "2", &ent->damage );
  1573.     G_SpawnFloat( "phase", "0", &phase );
  1574.  
  1575.     trap_SetBrushModel( ent, ent->model );
  1576.  
  1577.     // find pendulum length
  1578.     length = fabs( ent->r.mins[2] );
  1579.     if ( length < 8 ) {
  1580.         length = 8;
  1581.     }
  1582.  
  1583.     freq = 1 / ( M_PI * 2 ) * sqrt( g_gravity.value / ( 3 * length ) );
  1584.  
  1585.     ent->s.pos.trDuration = ( 1000 / freq );
  1586.  
  1587.     InitMover( ent );
  1588.  
  1589.     VectorCopy( ent->s.origin, ent->s.pos.trBase );
  1590.     VectorCopy( ent->s.origin, ent->r.currentOrigin );
  1591.  
  1592.     VectorCopy( ent->s.angles, ent->s.apos.trBase );
  1593.  
  1594.     ent->s.apos.trDuration = 1000 / freq;
  1595.     ent->s.apos.trTime = ent->s.apos.trDuration * phase;
  1596.     ent->s.apos.trType = TR_SINE;
  1597.     ent->s.apos.trDelta[2] = speed;
  1598. }
  1599.  
  1600. /*
  1601. ===============================================================================
  1602.  
  1603. GLASS
  1604.  
  1605. ===============================================================================
  1606. */
  1607.  
  1608. void G_ResetGlass ( void )
  1609. {
  1610.     gentity_t* ent;
  1611.     ent = NULL;
  1612.     while ( NULL != (ent = G_Find ( ent, FOFS(classname), "func_glass" ) ) )
  1613.     {
  1614.         trap_LinkEntity ( ent );
  1615.     }
  1616. }
  1617.  
  1618. void G_GlassDie ( gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int mod, int hitLocation, vec3_t hitDir )
  1619. {
  1620.     gentity_t *te;
  1621.     vec3_t dif;
  1622.  
  1623.     dif[0] = (self->r.absmax[0]+self->r.absmin[0])/2;
  1624.     dif[1] = (self->r.absmax[1]+self->r.absmin[1])/2;
  1625.     dif[2] = (self->r.absmax[2]+self->r.absmin[2])/2;
  1626.  
  1627.     // Make the client shatter the glass
  1628.     te = G_TempEntity( dif, EV_GLASS_SHATTER );
  1629.     te->s.eventParm = self->s.number;
  1630.     VectorCopy(self->r.maxs, te->s.origin);
  1631.     VectorCopy(self->r.mins, te->s.angles);
  1632.  
  1633.     // Only unlink the entity so we can reset it later
  1634.     trap_UnlinkEntity(self);
  1635. }
  1636.  
  1637. void G_GlassUse(gentity_t *self, gentity_t *other, gentity_t *activator)
  1638. {
  1639.     G_GlassDie(self, other, other, 100, MOD_UNKNOWN, HL_NONE, vec3_origin );
  1640. }
  1641.  
  1642. /*QUAKED func_glass (0 .5 .8) ?
  1643. Breakable glass
  1644. */
  1645. void SP_func_glass( gentity_t *ent ) 
  1646. {
  1647.     trap_SetBrushModel( ent, ent->model );
  1648.     InitMover( ent );
  1649.  
  1650.     ent->r.svFlags = SVF_GLASS_BRUSH;
  1651.  
  1652.     VectorCopy( ent->s.origin, ent->s.pos.trBase );
  1653.     VectorCopy( ent->s.origin, ent->r.currentOrigin );
  1654.     if (!ent->health)
  1655.     {
  1656.         ent->health = 1;
  1657.     }
  1658.  
  1659.     ent->moverState = MOVER_POS1;
  1660.  
  1661.     ent->takedamage = qtrue;
  1662.  
  1663.     ent->die = G_GlassDie;
  1664.     ent->use = G_GlassUse;
  1665. }
  1666.